package xyz.scootaloo.mono.data.entity.auth

import org.ktorm.database.Database
import org.ktorm.entity.Entity
import org.ktorm.entity.sequenceOf
import org.ktorm.global.insert
import org.ktorm.schema.*
import java.time.LocalDateTime

/**
 * 权限日志表对应的实体类
 *
 * 此实体类对应的DAO [AuthRoles]
 *
 * @author flutterdash@qq.com
 * @since 2021/8/3 12:48
 */
interface AuthLog : Entity<AuthLog> {
    companion object : Entity.Factory<AuthLog>()

    var id: Int               // 日志的唯一标识
    var name: String          // 日志操作名
    var desc: String          // 描述
    var uid: Int              // 用户
    var type: String          // 操作类型
    var success: Boolean      // 操作是否执行成功
    var date: LocalDateTime   // 此操作发生的日期
    var ip: String            // 发起此操作的客户端IP
}

object AuthLogs : Table<AuthLog>("auth_log") {
    private val id = int("id").primaryKey().bindTo { it.id }
    private val name = varchar("name").bindTo { it.name }
    private val uid = int("uid").bindTo { it.uid }
    private val date = datetime("date").bindTo { it.date }
    private val success = boolean("success").bindTo { it.success }
    private val desc = varchar("desc").bindTo { it.desc }
    private val type = varchar("type").bindTo { it.type }
    private val ip = varchar("ip").bindTo { it.ip }

    fun insert(log: AuthLog) {
        insert {
            set(it.name, log.name)
            set(it.uid, log.uid)
            set(it.date, log.date)
            set(it.success, log.success)
            set(it.desc, log.desc)
            set(it.type, log.type)
            set(it.ip, log.ip)
        }
    }
}

val Database.authLogs get() = this.sequenceOf(AuthLogs)
